<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$kaarigar       = array();
$kaarigarDetail = array();
$opening        = array();
$total          = array();
$kaarigarId     = isset($_REQUEST['kaarigarId']) ? $_REQUEST['kaarigarId'] : 0;
$fromDate       = isset($_REQUEST['kaarigarDate']) ? $_REQUEST['kaarigarDate'] : 0;
$toDate         = isset($_POST['toDateYear']) ? $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'] : 0;

$total['qty']         = 0;
$total['vadhGhat']    = 0;
$total['addVadhGhat'] = 0;
$total['crAmount']    = 0;
$total['drAmount']    = 0;
$total['totalAmt']    = 0;

$opening['particulars']         = "Opening";
$opening['debit']               = 0;
$opening['credit']              = 0;
$opening['vadhaaroGhatado']     = 0;
$opening['addDebit']            = 0;
$opening['addCredit']           = 0;
$opening['addVadhaaroGhataado'] = 0;
$opening['qty']                 = 0;
$opening['rate']                = 0;
$opening['crAmount']            = 0;
$opening['drAmount']            = 0;
$opening['totalAmt']            = 0;
if(isset($_POST['kaarigarId']) && $_POST['kaarigarId'] > 0)
{
  $selectKaarigarDetail = "SELECT kaarigardetail.kaarigardetailId,kaarigardetail.kaarigarMasterId,kaarigardetail.particulars,kaarigardetail.debit,
                                  kaarigardetail.credit,kaarigardetail.vadhaaroGhatado,kaarigardetail.addDebit,kaarigardetail.addCredit,
                                  kaarigardetail.addVadhaaroGhataado,kaarigardetail.qty,kaarigardetail.rate,kaarigardetail.crAmount,
                                  kaarigardetail.drAmount,kaarigarmaster.kaarigarMasterId,kaarigarmaster.kaarigarId,
	                                DATE_FORMAT(kaarigarmaster.kaarigarDate,'%Y-%m-%d') AS kaarigarMasterDate
                             FROM kaarigardetail
                             JOIN kaarigarmaster ON kaarigardetail.kaarigarMasterId = kaarigarmaster.kaarigarMasterId
                            WHERE kaarigardetail.kaarigarMasterId = ".$_GET['kaarigarMasterId']."
                              AND kaarigarmaster.kaarigarId = ".$kaarigarId."
                              AND kaarigarmaster.kaarigarDate <= '".$toDate."'
                            ORDER BY kaarigardetail.kaarigardetailId, kaarigardetail.kaarigarMasterId";
  $selectKaarigarDetailRes = mysql_query($selectKaarigarDetail);
  $b = 0;
  while($kaarigarDetailRow = mysql_fetch_assoc($selectKaarigarDetailRes))
  {
    if($kaarigarDetailRow['kaarigarMasterDate'] >= $fromDate)
    {
      $kaarigarDetail[$b]['kaarigardetailId'] = $kaarigarDetailRow['kaarigardetailId'];
      $kaarigarDetail[$b]['kaarigarMasterId'] = $kaarigarDetailRow['kaarigarMasterId'];
      $kaarigarDetail[$b]['particulars']     = $kaarigarDetailRow['particulars'];
      $kaarigarDetail[$b]['debit']           = $kaarigarDetailRow['debit'];
      $kaarigarDetail[$b]['credit']          = $kaarigarDetailRow['credit'];
      $kaarigarDetail[$b]['vadhaaroGhatado'] = $kaarigarDetailRow['vadhaaroGhatado'];
      $kaarigarDetail[$b]['addDebit']            = $kaarigarDetailRow['addDebit'];
      $kaarigarDetail[$b]['addCredit']           = $kaarigarDetailRow['addCredit'];
      $kaarigarDetail[$b]['addVadhaaroGhataado'] = $kaarigarDetailRow['addVadhaaroGhataado'];
      $kaarigarDetail[$b]['qty']             = $kaarigarDetailRow['qty'];
      $kaarigarDetail[$b]['rate']            = $kaarigarDetailRow['rate'];
      $kaarigarDetail[$b]['crAmount']        = $kaarigarDetailRow['crAmount'];
      $kaarigarDetail[$b]['drAmount']        = $kaarigarDetailRow['drAmount'];
      $kaarigarDetail[$b]['totalAmt']        = $kaarigarDetailRow['drAmount'] - $kaarigarDetailRow['crAmount'];
      $b++;
    }
    else
    {
      $opening['debit']           += $kaarigarDetailRow['debit'];
      $opening['credit']          += $kaarigarDetailRow['credit'];
      $opening['vadhaaroGhatado'] += $kaarigarDetailRow['vadhaaroGhatado'];
      $opening['addDebit']            += $kaarigarDetailRow['addDebit'];
      $opening['addCredit']           += $kaarigarDetailRow['addCredit'];
      $opening['addVadhaaroGhataado'] += $kaarigarDetailRow['addVadhaaroGhataado'];
      $opening['qty']             += $kaarigarDetailRow['qty'];
      $opening['rate']            += $kaarigarDetailRow['rate'];
      $opening['crAmount']        += $kaarigarDetailRow['crAmount'];
      $opening['drAmount']        += $kaarigarDetailRow['drAmount'];
      $opening['totalAmt']        += $kaarigarDetailRow['drAmount'];
    }
    
    $total['qty']      += $kaarigarDetailRow['qty'];
    $total['vadhGhat'] += $kaarigarDetailRow['vadhaaroGhatado'];
    $total['addVadhGhat'] += $kaarigarDetailRow['addVadhaaroGhataado'];
    $total['crAmount'] += $kaarigarDetailRow['crAmount'];
    $total['drAmount'] += $kaarigarDetailRow['drAmount'];
    $total['totalAmt'] += $kaarigarDetailRow['drAmount'] - $kaarigarDetailRow['crAmount'];
  }
}

$selectKaarigar = "SELECT kaarigarId,kaarigarName 
                     FROM kaarigar
                    ORDER BY kaarigarName";
$selectKaarigarRes = mysql_query($selectKaarigar);
$a = 0;
while($kaarigarRow = mysql_fetch_assoc($selectKaarigarRes))
{
  $kaarigar['id'][$a] = $kaarigarRow['kaarigarId'];
  $kaarigar['name'][$a] = $kaarigarRow['kaarigarName'];
  $a++;  
}

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_kaarigarName",$l_kaarigarName);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_vadhaaroGhataado",$l_vadhaaroGhataado);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_rate",$l_rate);
$smarty->assign("l_crAmount",$l_crAmount);
$smarty->assign("l_drAmount",$l_drAmount);
$smarty->assign("l_bhatthiDetail",$l_bhatthiDetail);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("l_edit",$l_edit);
$smarty->assign("l_crDr",$l_crDr);

$smarty->assign("kaarigar",$kaarigar);
$smarty->assign("kaarigarId",$kaarigarId);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("kaarigarDetail",$kaarigarDetail);
$smarty->assign("opening",$opening);
$smarty->assign("total",$total);
$smarty->assign("doSubmit",(isset($_POST['fromDateDay']) ? 0 : 1));
$smarty->display("kaarigarDetailList.tpl");
?>